import smtplib, ssl
from email.message import EmailMessage
from path import Path

def me():
    print("发起邮箱='18513117913@163.com',密码='Fleeting_Cen10'")
    print("发起邮箱='18513117913@163.com',赋权码='OROPBPZULYDMLGPZ',送达邮箱='18513117913@163.com'")

class mail_smtp():

    def __init__(self,发起邮箱,赋权码,送达邮箱,主题='',正文=''):
        self.message = EmailMessage()
        self.message['From'] = 发起邮箱
        self.message['To'] = 送达邮箱
        self.message['Subject'] = 主题
        self.message.set_content(正文)
        self.Ec = 赋权码
        self.smtp = 'smtp.'+发起邮箱[发起邮箱.find('@')+1:发起邮箱.find('.')]+'.com'
    # 附件传输
    def atth(self, atth, maintype='file'):
        sth = atth.replace('\\','/')
        dot_pos = sth[::-1].find('.')
        name_pos = sth[::-1].find('/')
        if(dot_pos < 0):
            dot_pos = len(sth)
        sub = sth[len(sth)-dot_pos:]

        if(name_pos < 0):
            name_pos = len(sth)
        file = sth[len(sth)-name_pos:]

        f = open(sth, "rb").read()
        self.message.add_attachment(f, maintype=maintype, subtype=sub, filename=file)  # 添加附件
    # 端口查看//懒狗用了别人总结好的
    def check_port(self):
        print("http://t.zoukankan.com/05-hust-p-14707724.html")
    # 发送
    def down(self, port, ssls='NONE'):
        if(ssls == 'NONE'):
            server = smtplib.SMTP(self.smtp, port=port)
        else:
            connt = ssl.create_default_context()
            server = smtplib.SMTP_SSL(self.smtp, port=port, context=connt)
        server.login(self.message['From'], self.Ec)
        server.send_message(self.message)

        server.quit()


PATH = Path().fp
report_path = PATH + r'TEST\Report\中央理财系统测试.html'

my = mail_smtp(发起邮箱='18513117913@163.com', 赋权码='OROPBPZULYDMLGPZ', 送达邮箱='18513117913@163.com')
my.atth(report_path)
my.down(25)
